#pragma once

#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>




#define CONV(n) (sockaddr*)(n)

//对于相应的结构体的封装
namespace InetAddrMoudle
{

    class InetAddr
    {
        public:

        InetAddr()
        {

        }

        InetAddr(struct sockaddr_in &addr):_addr(addr)
        {
            _port=ntohs(_addr.sin_port);
            _ip=inet_ntoa(_addr.sin_addr);
        }

        InetAddr(uint16_t port):_port(port),_ip()//这里的类型可能不匹配
        {
            _addr.sin_family=AF_INET;
            _addr.sin_port=htons(_port);
            _addr.sin_addr.s_addr=INADDR_ANY;
        }


        std::string Ip(){return _ip;}
        uint16_t Port(){return _port;}

        std::string PrintDebug()
        {
            std::string info=_ip;
            info+=" :";
            info+=std::to_string(_port);
            return info;
        }

        socklen_t AddrLen() { return sizeof(_addr); }

        const struct sockaddr* GetAddr()
        {
            return CONV(&_addr);
        }

        bool operator ==(const InetAddr &addr){
            return this->_ip==addr._ip&&this->_port==addr._port;
        }

        void SetAddr(const sockaddr_in &clinet,socklen_t &len)
        {
            _addr=clinet;

            _ip=inet_ntoa(clinet.sin_addr);

            (void)len;
        }
        
        ~InetAddr(){}
        
        private:
        std::string _ip;
        uint16_t _port;
        
        struct sockaddr_in _addr;
    };

}